package flc.ast.fragment;

import androidx.fragment.app.Fragment;

import android.view.View;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import flc.ast.R;
import flc.ast.databinding.FragmentTimeConvertBinding;
import flc.ast.utils.DateAndTimeDialog;
import flc.ast.utils.LunarUtil;
import stark.common.basic.base.BaseNoModelFragment;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TimeConvertFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TimeConvertFragment extends BaseNoModelFragment<FragmentTimeConvertBinding> implements DateAndTimeDialog.TimePickerDialogInterface {

    private DateAndTimeDialog dateAndTimeDialog;
    private Date dateConvert;


    @Override
    protected int onCreate() {
        return R.layout.fragment_time_convert;
    }

    @Override
    protected void initView() {
        Date currentDate = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        dateConvert=currentDate;
        try {
            calculate();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        mDataBinding.tvTimeConvert.setText(dateFormat.format(currentDate));
        mDataBinding.llTimeConvert.setOnClickListener(this);
        mDataBinding.ivStartCal.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.llTimeConvert:
                dateAndTimeDialog=new DateAndTimeDialog(getContext(),R.id.llTimeConvert);
                dateAndTimeDialog.showDatePickerDialog();
                break;
            case R.id.ivStartCal:
                try {
                    calculate();
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                break;
        }
    }

    public void myPositiveListener(int viewCode) throws ParseException {
        SimpleDateFormat stringFormat = new SimpleDateFormat("yyyy/MM/dd");
        Date date = stringFormat.parse(dateAndTimeDialog.getDate());
        dateConvert = date;
        mDataBinding.tvTimeConvert.setText(dateAndTimeDialog.getDate());
    }

    @Override
    protected void initData() {

    }

    private void calculate() throws ParseException {
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(dateConvert);
        LunarUtil lunarUtil=new LunarUtil(calendar);
        mDataBinding.tvTimeConvertRet.setText(lunarUtil.toString()+"");
    }

    @Override
    public void positiveListener(int viewCode) {

    }

    @Override
    public void negativeListener() {

    }
}